from flask import render_template, session, jsonify, request, redirect
import time
from views import shop_blue
from models import db
from models.index import Shoping_car, Order_form
from utils.index import if_user


# 购物车页面
@shop_blue.route('/shop/shop_car')
def shop_car():
    # 获取去用户对象 并判断是否登录
    user = if_user()

    if not user:
        return redirect('/registered/login')

    # 打印购物车对象
    count_price = 0
    for shop_car in user.shoping_car:
        shop_car.commodity.price = int(shop_car.commodity.price)
        shop_car.shoping_num = int(shop_car.shoping_num)
        # print('------------', shop_car.commodity)
        count_price += shop_car.commodity.price * shop_car.shoping_num
        # print('------------', count_price)
    # shop_car.commodity.price * shop_car.shoping_num

    return render_template('Shop_cart.html', user=user, count_price=count_price)


# 购物车逻辑
@shop_blue.route('/shop/shop_car_from', methods=['POST'])
def shop_car_from():
    # 获取用户对象
    user = if_user()

    # 获取购物车数据的id
    shop_car_id = request.json.get('shop_car_id')

    ret = {
        "errno": 5002,
        "errmsg": "购买错误"
    }

    # 判断选中的购物车数据对象 将其放入shop_car_id_cont
    shop_car_id_count = []
    for x in shop_car_id:
        y = db.session.query(Shoping_car).filter(Shoping_car.id == x).first()
        # print(y)
        if y:
            shop_car_id_count.append(y)

    if not shop_car_id_count:
        ret = {
            "errno": 5001,
            "errmsg": "未选择购买物品"
        }
        return jsonify(ret)

    # 获取订单号
    time_num = time.time()
    order_count = user.order.count()
    production_num = str(time_num).replace('.', '') + str(user.id) + str(order_count)
    print('+++++++++++++++++++++++++', production_num)

    # 添加数据  将购买的物品添加到订单表中
    for x in shop_car_id_count:
        if x:
            y = Order_form()
            y.production_num = production_num
            y.commodity_id = x.commodity_id
            y.user_id = x.user_id
            y.shoping_num = x.shoping_num
            y.price_count = int(x.commodity.price) * int(x.shoping_num)

            # 在订单表中添加购买的物品
            db.session.add(y)
            # 删除购物车表中已经购买的物品
            db.session.delete(x)
            # 更新数据库
            db.session.commit()

            ret = {
                "errno": 5000,
                "errmsg": "购买成功"
            }

    return jsonify(ret)


# 删除购物车物品逻辑
@shop_blue.route('/shop/shop_car_delete', methods=['POST'])
def shop_car_delete():
    # 获取用户对象
    user = if_user()

    # 获取购物车数据的id
    shop_car_id = request.json.get('shop_car_id')
    print('++++++++++++++++++++++', shop_car_id)

    # 用于判断是否未选择
    if_null = False
    for x in shop_car_id:
        if x:
            if_null = True
    if not if_null:
        ret = {
            "errno": 5006,
            "errmsg": "未选择商品"
        }
        return jsonify(ret)

    for x in shop_car_id:
        # 获取购物车数据对象
        shop_car = db.session.query(Shoping_car).filter(Shoping_car.id == x).first()
        # 对象存在  删除数据
        if shop_car:
            db.session.delete(shop_car)
            db.session.commit()
            ret = {
                "errno": 5005,
                "errmsg": "删除成功"
            }
        print('++++++++++++++++++++++', shop_car)

    return jsonify(ret)
